package com.jic.data.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jic.data.constant.consist.ResponseCode;
import com.jic.data.exception.AppRuntimeException;
import com.jic.data.mapper.MemberLevelPointsStsBaseMapper;
import com.jic.data.model.entity.MemberIsconsumeSts;
import com.jic.data.model.entity.MemberLevelPointsStsBase;
import com.jic.data.model.entity.MemberRegisterSts;
import com.jic.data.service.MemberLevelPointsStsBaseService;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 接口实现类
 *
 * @author : 建投数据科技(山东)有限公司
 * @version : v1.0.0
 * @email :
 * @since : 2020-03-09 10:12:20
 */
@Service
@Slf4j
public class MemberLevelPointsStsBaseServiceImpl implements MemberLevelPointsStsBaseService {

    @Resource
    private MemberLevelPointsStsBaseMapper mapper;


    /**
     * 新增记录
     *
     * @param request 需要保存的入参实体类
     * @return int 是否保存成功
     */
    @Override
    public int save(MemberLevelPointsStsBase request) {
        int flag = mapper.insert(request);
        if (flag == 1) {
            return 1;
        } else {
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "新增失败");
        }
    }


    /**
     * 保存或修改记录信息
     *
     * @param request 需要保存的入参实体类
     * @return int 是否保存或修改成功
     * author 建投数据科技(山东)有限公司
     * @date 2020-3-11 20:54:09
     */
    @Override
    public int saveOrUpdate(MemberLevelPointsStsBase request) {
        log.info("保存或修改记录信息，入参 = {}", JSONObject.toJSONString(request));
        try {
            if (request == null) {
                throw new AppRuntimeException(ResponseCode.EXCEPTION, "入参为空");
            }
            if (request.getId() != null && request.getId() != 0) {
                log.info("保存或修改记录信息，修改，入参 = {}", JSONObject.toJSONString(request));
                int flag = mapper.updateByPrimaryKey(request);
                if (flag == 1) {
                    return 1;
                }
            } else {
                log.info("保存或修改记录信息，保存，入参 = {}", JSONObject.toJSONString(request));
                int flag = mapper.insertSelective(request);
                if (flag == 1) {
                    return 1;
                } else {
                    throw new AppRuntimeException(ResponseCode.EXCEPTION, "修改失败");
                }
            }
        } catch (Exception e) {
            log.error("用户保存或修改失败", e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "用户保存或修改失败");
        }
        return -1;
    }

    /**
     * 删除记录
     *
     * @param request 需要删除,含有主键的入参实体类
     * @return int 是否删除成功
     */
    @Override
    public int deleteByPrimaryKey(MemberLevelPointsStsBase request) {
        int flag = mapper.deleteByPrimaryKey(request);
        if (flag == 1) {
            return 1;
        } else {
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "删除失败");
        }
    }


    /**
     * 逻辑删除记录
     *
     * @param request 含有主键的逻辑删除实体类
     * @return int 是否逻辑删除成功
     */
    @Override
    public int deleteByPrimaryKeyLogically(MemberLevelPointsStsBase request) {
        request.setDeleteFlag(1);
        int flag = mapper.updateByPrimaryKey(request);
        if (flag == 1) {
            return 1;
        } else {
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "逻辑删除记录失败");
        }
    }


    /**
     * 修改
     *
     * @param request
     * @return
     */
    @Override
    public int updateByPrimaryKeySelective(MemberLevelPointsStsBase request) {
        try {
            log.info("修改记录");
            MemberLevelPointsStsBase param = new MemberLevelPointsStsBase();
            BeanUtils.copyProperties(request, param);
            int flag = mapper.updateByPrimaryKeySelective(param);
            if (flag == 1) {
                return 1;
            } else {
                throw new AppRuntimeException(ResponseCode.EXCEPTION, "修改记录失败");
            }
        } catch (Exception e) {
            log.error("修改记录失败", e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "修改记录失败");
        }

    }

    /**
     * 记录详情
     *
     * @param request 查询的入参实体类
     * @return 数据库查询到的实体类
     */
    @Override
    public MemberLevelPointsStsBase getByPrimaryKey(MemberLevelPointsStsBase request) {
        return mapper.selectByPrimaryKey(request);
    }


    /**
     * 分页查询
     *
     * @param request 分页查询入参实体类
     * @return 含有分页信息的实体类列表
     */
    @Override
    public PageInfo<MemberLevelPointsStsBase> listPageBySelective(MemberLevelPointsStsBase request, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<MemberLevelPointsStsBase> list = mapper.listPageBySelective(request);
        return new PageInfo(list);
    }

    /**
     * 不分页查询
     *
     * @param request 不分页查询入参实体类
     * @return 数据库查到的实体类列表
     */
    @Override
    public List<MemberLevelPointsStsBase> listAllRecord(MemberLevelPointsStsBase request) {
        //分页处理
        request.setDeleteFlag(0);
        return mapper.selectBySelective(request);
    }

    /**
     * 会员等级-柱状图
     * @param request 查询入参实体类
     * @return 实体类列表
     * @author tzc
     * @date 2020-5-21 10:50:14
     */
    @Override
    public Map<String,Integer> getCountOfMemberLevel(MemberLevelPointsStsBase request) {
//分页处理
        request.setDeleteFlag(0);
        List<MemberLevelPointsStsBase> list = mapper.selectBySelective(request);
        Integer diamond2Level = 0;
        Integer diamondLevel = 0;
        Integer normalLevel = 0;
        Integer fansLevel = 0;
        for (MemberLevelPointsStsBase item : list) {
            //已经消费的人数
            if (item.getLevelId() != null && item.getLevelId() ==4){
                diamond2Level = diamond2Level + item.getLevelMemberNumber();
            }
            if (item.getLevelId() != null && item.getLevelId() ==3){
                diamondLevel = diamondLevel + item.getLevelMemberNumber();
            }
            if (item.getLevelId() != null && item.getLevelId() ==2){
                normalLevel = normalLevel + item.getLevelMemberNumber();
            }
            if (item.getLevelId() != null && item.getLevelId() ==1){
                fansLevel = fansLevel + item.getLevelMemberNumber();
            }
        }

        Map<String,Integer> map = new HashMap<>();
        map.put("diamond2Level",diamond2Level);
        map.put("diamondLevel",diamondLevel);
        map.put("normalLevel",normalLevel);
        map.put("fansLevel",fansLevel);
        return map;
    }
    /**
     * 会员等级 - 表单
     * @param request 查询入参实体类
     * @return 实体类列表
     * @author tzc
     * @date 2020-5-21 10:50:14
     */
    @Override
    public MemberLevelPointsStsBase getDataOfMemberPoint(MemberLevelPointsStsBase request) {

        MemberLevelPointsStsBase point = new MemberLevelPointsStsBase();
        Integer allPoint = 0;
        Integer allAvailablePoint = 0;
        Integer allUsedPoint = 0;
        Double allAvgPoint = 0.0;

        request.setDeleteFlag(0);
        List<MemberLevelPointsStsBase> list = mapper.selectBySelective(request);
        for (MemberLevelPointsStsBase item:list) {
            allAvailablePoint = allAvailablePoint + item.getAllLevelsAvailablePoints();
            allPoint = allPoint + item.getLevelTotalPoints();
            allUsedPoint = allUsedPoint + item.getLevelUsedPoints();
            allAvgPoint = allAvgPoint + item.getLevelMemberAvgAvailablePoints();
        }

        point.setAllLevelsAvailablePoints(allAvailablePoint);
        point.setLevelTotalPoints(allPoint);
        point.setLevelUsedPoints(allUsedPoint);
        point.setLevelMemberAvgAvailablePoints(allAvgPoint);
        return point;
    }


    /**
     * 不分页查询
     *
     * @param request 不分页查询入参实体类
     * @return 数据库查到的实体类列表
     */
    @Override
    public Map<String,Integer> getAvailablePointByGroup(MemberLevelPointsStsBase request) {
        request.setDeleteFlag(0);
        List<MemberLevelPointsStsBase> list = mapper.selectBySelective(request);
        Integer less1000  = 0;
        Integer total1000To2000 = 0;
        Integer total2000To3000 = 0;
        Integer total3000To4000  = 0;
        Integer total4000To5000 = 0;
        Integer total5000To6000 = 0;
        Integer total6000To7000 = 0;
        Integer total7000To8000 = 0;
        Integer total8000To9000 = 0;
        Integer total9000To10000 = 0;
        Integer more10000 = 0;

        for (MemberLevelPointsStsBase item : list) {
            Integer point = item.getAllLevelsAvailablePoints();
            Integer num = item.getAllLevelsMemberNumber();
            if (point< 1000){
                less1000 = less1000 + num;
            }
            else if (1000<=point  && point<=2000){
                total1000To2000 = total1000To2000 + num;
            }
            else if (2000 <= point  && point<=3000){
                total2000To3000 = total2000To3000 + num;
            }
            else if (3000 <=point  && point<=4000){
                total3000To4000 = total3000To4000 + num;
            }
            else if (4000 <=point  && point<= 5000){
                total4000To5000 = total4000To5000 + num;
            }
            else if (5000 <=point  && point<=6000){
                total5000To6000 = total5000To6000 + num;
            }
            else if (6000 <=point  && point<= 7000){
                total6000To7000 = total6000To7000 + num;
            }
            else if (7000 <=point  && point<= 8000){
                total7000To8000 = total7000To8000 + num;
            }
            else if (8000 <=point  && point<= 9000){
                total8000To9000 = total8000To9000 + num;
            }
            else if (9000 <=point  && point<= 10000){
                total9000To10000 = total9000To10000 + num;
            }
            else if (10000 <=point ){
                more10000 = more10000 + num;
            }
        }

        Map<String,Integer> map = new HashMap<>();
        map.put("less1000",less1000);
        map.put("total1000To2000",total1000To2000);
        map.put("total2000To3000",total2000To3000);
        map.put("total3000To4000",total3000To4000);
        map.put("total4000To5000",total4000To5000);
        map.put("total5000To6000",total5000To6000);
        map.put("total6000To7000",total6000To7000);
        map.put("total7000To8000",total7000To8000);
        map.put("total8000To9000",total8000To9000);
        map.put("total9000To10000",total9000To10000);
        map.put("more10000",more10000);

        return map;
    }

}
